package org.sunrise.game.game.logic;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;

public class ToolsUtils {
    private static long todayZeroTimeMillis;
    private static long weekZeroTimeMillis;
    public static final long DAY_MILLS = 24 * 60 * 60 * 1000L;
    public static final long HOUR_MILLS = 60 * 60 * 1000L;
    public static final long MINUTE_MILLS = 60 * 1000L;
    public static final long SEC_MILLS = 1000L;

    /**
     * 获取今日零点时间戳
     */
    public static long getTodayZeroTimeMillis() {
        if (todayZeroTimeMillis == 0 || System.currentTimeMillis() - todayZeroTimeMillis >= DAY_MILLS) {
            LocalDate today = LocalDate.now();

            // 转换为今天的00:00:00
            ZonedDateTime midnightZoned = today.atStartOfDay(ZoneId.systemDefault());

            todayZeroTimeMillis = midnightZoned.toInstant().toEpochMilli();
        }
        return todayZeroTimeMillis;
    }

    /**
     * 获取时间戳 通过偏移量 以今日0点为基础单位
     */
    public static long getDayTimeMillisByOffset(int day) {
        return getTodayZeroTimeMillis() + day * DAY_MILLS;
    }

    public static long getDayTimeMillisByOffset(int day, int hour) {
        return getTodayZeroTimeMillis() + day * DAY_MILLS + hour * HOUR_MILLS;
    }

    public static long getDayTimeMillisByOffset(int day, int hour, int minute) {
        return getTodayZeroTimeMillis() + day * DAY_MILLS + hour * HOUR_MILLS + minute * MINUTE_MILLS;
    }

    public static long getDayTimeMillisByOffset(int day, int hour, int minute, int sec) {
        return getTodayZeroTimeMillis() + day * DAY_MILLS + hour * HOUR_MILLS + minute * MINUTE_MILLS + sec * SEC_MILLS;
    }

    /**
     * 获取本周零点时间戳
     */
    public static long getWeekZeroTimeMillis() {
        if (weekZeroTimeMillis == 0 || System.currentTimeMillis() - todayZeroTimeMillis >= 7 * DAY_MILLS) {
            // 获取今天的日期
            LocalDate today = LocalDate.now();

            // 获取本周一的日期（本周一是当前日期所在周的第一个工作日，通常是周一）
            LocalDate monday = today.with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY));

            // 将本周一的日期转为当地时区的零点时间（00:00:00）
            ZonedDateTime mondayMidnight = monday.atStartOfDay(ZoneId.systemDefault());

            // 返回本周一零点的毫秒级时间戳
            weekZeroTimeMillis = mondayMidnight.toInstant().toEpochMilli();
        }
        return weekZeroTimeMillis;
    }

    /**
     * 获取时间戳 通过偏移量 以本周一0点为基础单位
     */
    public static long getWeekZeroTimeMillisByOffset(int day) {
        return getWeekZeroTimeMillis() + day * DAY_MILLS;
    }

    public static long getWeekZeroTimeMillisByOffset(int day, int hour) {
        return getWeekZeroTimeMillis() + day * DAY_MILLS + hour * HOUR_MILLS;
    }

    public static long getWeekZeroTimeMillisByOffset(int day, int hour, int minute) {
        return getWeekZeroTimeMillis() + day * DAY_MILLS + hour * HOUR_MILLS + minute * MINUTE_MILLS;
    }

    public static long getWeekZeroTimeMillisByOffset(int day, int hour, int minute, int sec) {
        return getWeekZeroTimeMillis() + day * DAY_MILLS + hour * HOUR_MILLS + minute * MINUTE_MILLS + sec * SEC_MILLS;
    }
}
